﻿#define NOMINMAX

#include <chrono>
#include <fstream>
#include <iostream>
#include <thread>

#include "../src/util/time_util.hh"
#include "box/service_box.hh"
#include "box/service_context.hh"
#include "detail/lang_impl.hh"

void setup() {
  {// proxy
    std::ofstream ofs;
    ofs.open("config_service_box_proxy.cfg", std::ios::trunc | std::ios::out);
    ofs << "listener = {\r"
           "   host = (\"127.0.0.1:10003\")\r"
           "}\r"

           "box_channel_recv_buffer_len = 1024\r"
           "box_name = \"guild_service\"\r"
           "logger_config_line = "
           //  "\"file:\/\/.;file=box_%y%m%d.log;line=[%y%m%d-%H:%M:%S:%U];flush="
           //  "true;mode=day;maxSize=10240000\"\r"
           "\"console://.;line=[%y%m%d-%H:%M:%S:%U];flush=true\"\r"
           
           "service = {\r"
           "   service_dir = "
#ifdef _WIN32
           "\"../../../../../../../publish/bin/service/Debug\"\r"
#else
           "\"../../../../../../../publish/bin/service\"\r"
#endif // _WIN32
          "}"

           "debugger = {\r"
           "   enable = \"true\"\r"
           "   listen = \"0.0.0.0:10888\"\r"
           "}\r"

           "open_coroutine = \"true\"\r"

           "service_finder = {\r"
           "   type = \"zookeeper\"\r"
           "   hosts = \"127.0.0.1:2181\"\r"
           "}\r"

           "statistics = {\r"
           "   is_open_rpc_stat = \"true\"\r"
           "}\r"

            "proxy = {\r"
            "   query_timeout = 5000\r"
            "   call_timeout = 2000\r"
            "   seed = 1\r"
            "   listener = (\"0.0.0.0:9888\")\r"
            "}\r"
            ;
    ofs.close();

  }
}

void run() {
  kratos::service::ServiceBox service_proxy;
  const char *argv_service[] = {"guild_service_proxy",
                                "--config=config_service_box_proxy.cfg", "--proxy"};

  if (!service_proxy.start(3, argv_service)) {
    return;
  }

  auto start = kratos::util::get_os_time_millionsecond();
  while (true) {
    if (service_proxy.is_wait_stop()) {
      break;
    }

    service_proxy.update();

    std::this_thread::sleep_for(std::chrono::milliseconds(1));
  }

  // std::this_thread::sleep_for(std::chrono::milliseconds(1000));

  service_proxy.stop();
}

int main(int argc, char *argv[]) {
  setup();

  run();

  return 0;
}
